package com.sunday.pinxinghui.model.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.BetterRecyclerView;
import com.sunday.common.widgets.CircleImageView;
import com.sunday.pinxinghui.MainActivity;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.adapter.CommonAdapter;
import com.sunday.pinxinghui.adapter.ViewHolder;
import com.sunday.pinxinghui.base.BaseApplication;
import com.sunday.pinxinghui.entity.LifeData;
import com.sunday.pinxinghui.entity.Member;
import com.sunday.pinxinghui.model.account.RechargeActivity;
import com.sunday.pinxinghui.network.ApiClient;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/11/12.
 */

public class MemberCenterActivity extends BaseActivity {


    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.common_header)
    RelativeLayout commonHeader;
    @Bind(R.id.user_logo)
    CircleImageView userLogo;
    @Bind(R.id.user_name)
    TextView userName;
    @Bind(R.id.user_need_money)
    TextView userNeedMoney;
    @Bind(R.id.txt)
    TextView txt;
    @Bind(R.id.life_tips)
    TextView lifeTips;
    @Bind(R.id.header_layout)
    LinearLayout headerLayout;
    @Bind(R.id.privilege_recyclerview)
    BetterRecyclerView privilegeRecyclerview;
    @Bind(R.id.btn_recharge)
    TextView btnRecharge;
    @Bind(R.id.btn_buy)
    TextView btnBuy;
    @Bind(R.id.scrollView)
    ScrollView scrollView;

    private CommonAdapter<String> imgAdapter;
    private List<String> imgs = new ArrayList<>();
    private Member member;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_center);
        ButterKnife.bind(this);
        txt.setFocusable(true);
        txt.setFocusableInTouchMode(true);
        txt.requestFocus();
        titleView.setText("会员中心");
        member = BaseApplication.getInstance().getMember();
        initView();
        getLifeData();
    }


    private void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        privilegeRecyclerview.setLayoutManager(layoutManager);
        imgAdapter = new CommonAdapter<String>(mContext, R.layout.list_previllage_item, imgs) {
            @Override
            public void convert(ViewHolder holder, String s, int position) {
                ImageView img = holder.getView(R.id.img);
                Glide.with(mContext)
                        .load(s)
                        .error(R.mipmap.ic_default)
                        .into(img);
            }
        };
        privilegeRecyclerview.setAdapter(imgAdapter);

        Glide.with(mContext)
                .load(member.getLogo())
                .error(R.mipmap.ic_default)
                .into(userLogo);
        userName.setText(member.getNickName());
        userNeedMoney.setText("已消费0元");

    }

    @OnClick({R.id.btn_recharge, R.id.btn_buy})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_recharge:
                intent = new Intent(mContext, RechargeActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_buy:
                intent = new Intent(mContext, MainActivity.class);
                startActivity(intent);
                break;
        }
    }


    private void getLifeData() {
        showLoadingDialog(0);
        Call<ResultDO<LifeData>> call = ApiClient.getApiAdapter().getLifeData(BaseApplication.getInstance().getMember().getId());
        call.enqueue(new Callback<ResultDO<LifeData>>() {
            @Override
            public void onResponse(Call<ResultDO<LifeData>> call, Response<ResultDO<LifeData>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                dissMissDialog();
                if (response.body().getCode() == 0) {
                    LifeData lifeData = response.body().getResult();
                    lifeTips.setText(response.body().getResult().getTips());
                    imgs.clear();
                    imgs.addAll(response.body().getResult().getImages());
                    imgAdapter.notifyDataSetChanged();
                    if (member.getIsVip() == 1) {
                        userNeedMoney.setText("品行会员");
                    } else {
                        userNeedMoney.setText(lifeData.getShortMoney().compareTo(BigDecimal.ZERO) > 0 ?
                                String.format("已消费%1$s元，本月还需消费%2$s元成为会员", lifeData.getBuyMoney().setScale(2, RoundingMode.HALF_UP),
                                        lifeData.getShortMoney().setScale(0, RoundingMode.HALF_UP)) : "品行会员");
                    }


                }
            }


            @Override
            public void onFailure(Call<ResultDO<LifeData>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }


}
